package com.wider.foundation.Nx;

import java.io.File;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;



public class NxFile {
//spec
	private NxFS filesys;
	private File path;
	private boolean isfolder;
	private long lastmod;
	private long filesize;
	private NxFiles list;
//taiwei add
	private boolean delete;
	private boolean exists;


	//Constructors 	
	public NxFile(NxFS fs,File parent,String child){
		filesys=fs;
		path=new File(parent,child);
	}
	public NxFile(NxFS fs, String pathname){
		filesys=fs;
		path=new File(pathname);
	}
	public NxFile(NxFS fs ,String parent,String child){
		filesys=fs;
		path=new File(parent,child);
	}

	//public Method
	//Spec Methods
	
//	public boolean delete(NxAsynchronous handler){}
	public boolean exists(NxAsynchronous handler){
		FShandler fshandler=new FShandler();
		if(handler != null){
			filesys.getInfo(this, fshandler);
			handler.NxUpdate(this, exists, isfolder, lastmod, filesize);
		}
		else
			filesys.getInfo(this, fshandler);
		return exists;
	}
	public boolean isDirectory(NxAsynchronous handler){
		FShandler fshandler=new FShandler();
		if(handler != null){
			filesys.getInfo(this, fshandler);
			handler.NxUpdate(this, exists, isfolder, lastmod, filesize);
		}
		else
			filesys.getInfo(this, fshandler);
		
		return isfolder;
		
	}
	public boolean isFile(NxAsynchronous handler){
		FShandler fshandler=new FShandler();
		if(handler != null){
			filesys.getInfo(this, fshandler);
			handler.NxUpdate(this, exists, isfolder, lastmod, filesize);
		}
		else
			filesys.getInfo(this, fshandler);
		
		
		return !isfolder;
	}
	public long lastModified(NxAsynchronous handler){
		FShandler fshandler=new FShandler();
		if(handler != null){
			filesys.getInfo(this, fshandler);
			handler.NxUpdate(this, exists, isfolder, lastmod, filesize);
		}
		else
			filesys.getInfo(this, fshandler);
		
		
		return lastmod;
	}
	public long length(NxAsynchronous handler){
		FShandler fshandler=new FShandler();
		if(handler != null){
			filesys.getInfo(this, fshandler);
			handler.NxUpdate(this, exists, isfolder, lastmod, filesize);
		}
		else
			filesys.getInfo(this, fshandler);
		return filesize;
	}
	
	
	
	
	public NxFiles listFiles(NxAsynchronous handler,NxFileFilter filter){
		
		return filesys.list(this, null);
	}
	
	
	//taiwei add
	public String getPath(){
		return path.getAbsolutePath();
	}
	public void show(){
		System.out.println("path="+path.toString()+" isfolder="+isfolder+" lastmod="+lastmod
				+" filesize="+filesize+" exists="+exists);
		
	}
	//Methods
	



	class FShandler implements NxAsynchronous{
		
		public void NxUpdate(NxFile Npath, boolean Nexists, boolean Nisfolder,	long Nlastmod, long Nfilelength) {
			exists=Nexists;
			filesize=Nfilelength;
			isfolder=Nisfolder;
			lastmod=Nlastmod;
		}
		
		public void NxUpdate(NxFile path, NxFiles files) {}
			
	}




}
